package com.orangelife.main;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.JsonToMapHelper;
import com.curry.android.util.YcToastHelper;
import com.fjycnet.eventbus._TestEventBusActivity;
import com.fjycnet.file.MyFileActivity;
import com.fjycnet.location.LocationActivity;
import com.fjycnet.qiniu.QiniuActivity;
import com.other.printer.PrinterActivity;
import com.ui.BRVAH.BrvahActivity;
import com.ui.LabelLayout.LabelLayoutActivity;
import com.ui.recyclerview.PageRecyclerView.TestPageRvActivity;
import com.ui.PickerView.JsonDataActivity;
import com.fjycnet.webview.WvActivity;
import com.fjycnet.webview.WvJsActivity;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.orangelife.R;
import com.fjycnet.intent.IntentHelperDemo;
import com.ui.recyclerview.PagerLayoutManager.TestPagerLayoutManagerAct;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView textView3;
    private ListView listView;
    private TextView textView2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        testVolley();
        testJson();
    }

    public static String[] getCertMsg(String packageName, Context cxt) {
        String[] certMsg = new String[2];
        PackageInfo pis;
        try {
            pis = cxt.getPackageManager().getPackageInfo(cxt.getPackageName(),
                    PackageManager.GET_SIGNATURES);
            Signature[] sigs = pis.signatures; // 签名
            CertificateFactory certFactory = CertificateFactory
                    .getInstance("X.509");
            // 获取证书
            X509Certificate cert = (X509Certificate) certFactory
                    .generateCertificate(new ByteArrayInputStream(sigs[0]
                            .toByteArray()));
            // 可根据证书发行者来判断该应用是否被二次打包（被破解的应用重新打包后，签名与原包一定不同，据此可以判断出该应用是否被人做过改动）
            certMsg[0] = cert.getIssuerDN().toString();  //证书发行者
            Log.e(MainActivity.class.getSimpleName(), "certMsg0=" + String.valueOf(certMsg[0]));
            certMsg[1] = cert.getSubjectDN().toString();
            Log.e(MainActivity.class.getSimpleName(), "certMsg1=" + String.valueOf(certMsg[1]));
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return certMsg;
    }

    private void initView() {
//        textView2 = findViewById(R.id.textView2);
//        String s2 = "<p>选项<span style='color: #ff00ff; font-size: 99px;'>C</span></p>";
//        Spanned result = Html.fromHtml(s2, null, new CustomTagHandler(this, textView2.getTextColors()));
//        Spanned result = Html.fromHtml(s2, null, null);
//        textView2.setText(result);
        String[] msg = getCertMsg("com.tencent.mm", this);


        button = findViewById(R.id.button);
        listView = findViewById(R.id.listView);
        //TODO 1 做成树形控件 2 使用JSON来配置菜单(路由Activity还是没问题的,不用Fragment,是为了便于迁移出去)
        ArrayList<String> list = new ArrayList<>();
        final SparseArray<Class> array = new SparseArray<>();
        list.add("0 学习EventBus");
        array.append(0, _TestEventBusActivity.class);
        list.add("1 学习WebView");
        array.append(1, WvActivity.class);
        list.add("2 测试PickerView");
        array.append(2, JsonDataActivity.class);
        list.add("3 学习WebView注入JS");
        array.append(3, WvJsActivity.class);
        list.add("4 测试连接热敏打印机");
        array.append(4, PrinterActivity.class);
        list.add("5 Test PageRvActivity");
        array.append(5, TestPageRvActivity.class);
        list.add("6 测试相册与相机功能");
        array.append(6, MyFileActivity.class);
        list.add("7 测试七牛上传图片");
        array.append(7, QiniuActivity.class);
        list.add("8 PagerLayoutManager");
        array.append(8, TestPagerLayoutManagerAct.class);
        list.add("9 LabelLayout标签布局");
        array.append(9, LabelLayoutActivity.class);
        list.add("10 BRVAH的测试");
        array.append(10, BrvahActivity.class);
        list.add("11 定位能力");
        array.append(11, LocationActivity.class);
//        HashMap<String, Object> map1 = new HashMap<>();
//        map1.put("name", "1 EventBus测试");
//        map1.put("class", _TestEventBusActivity.class);
//        HashMap<String, Object> map2 = new HashMap<>();
//        map2.put("2 WebView测试", "2 WebView测试");
//        map2.put("2 WebView测试", WvActivity.class);
//        HashMap<String, Object> map3 = new HashMap<>();
//        map3.put("3 PickerView测试", JsonDataActivity.class);
//        map3.put("3 PickerView测试", JsonDataActivity.class);
//        HashMap<String, Object> map4 = new HashMap<>();
//        map4.put("4 WebView注入JS", WvJsActivity.class);
//        map4.put("4 WebView注入JS", WvJsActivity.class);
//        HashMap<String, Object> map5 = new HashMap<>();
//        map5.put("5 热敏打印机测试", PrinterActivity.class);
//        map5.put("5 热敏打印机测试", PrinterActivity.class);

        listView.setAdapter(new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1,
                list));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                IntentHelper.getInstances().getIntent(MainActivity.this, array.get(i));

            }
        });
    }

    private void testVolley() {
        VolleyHttpUtil.getInstance().setDebug(true);
        VolleyHttpUtil.getInstance().request("www.google.com.hk", Request.Method.GET, null, null, null, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                YcToastHelper.get()._toast("errCode=" + errCode + ",response=" + response);
            }
        }, null, 0, null, new DefaultRetryPolicy(1000, 1, 1f));
//        VolleyHttpUtil.get().getWithCallback("www.baidu.com", new VolleyHttpUtil.ResponseCallBack() {
//            @Override
//            public void handleResponse(JSONObject response, int errCode) {
//                Log.e(MainActivity.class.getSimpleName(), response.toString());
//            }
//        });
    }

    private void testJson() {
        textView3 = (TextView) findViewById(R.id.textView3);
//        String json = "{\"remark\":\"1、修复部分BUG\n2、法克鱿！！！\",\"url\":\"http:\\/\\/www.17orange.com\\/downloads\\/orangelife\\/OrangeLife-3-8-1.apk\"}";
        String json = "{\"\nremark\":\"\n1\n2\n3\n4\n\"}";
        textView3.setText(json.toString());
        Log.e(MainActivity.class.getSimpleName(), "json=" + json);


        //test2
        JSONObject jo1 = null;
        try {
            jo1 = new JSONObject(json);
            Log.e(MainActivity.class.getSimpleName(), "jo1=" + jo1.toString());
            jo1.get("\nremark");
            Log.e(MainActivity.class.getSimpleName(), "jo1=" + jo1.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Map<String, Object> map2 = new Gson().fromJson(jo1.toString(), new TypeToken<Map<String, Object>>() {
        }.getType());
        Log.e(MainActivity.class.getSimpleName(), "map2=" + map2.toString());


        //test3
        com.google.gson.JsonObject jo2;
        jo2 = new JsonParser().parse(json).getAsJsonObject();
        Log.e(MainActivity.class.getSimpleName(), "jo2=" + jo2.toString());
        JsonElement je = jo2.get("\nremark");
        String strJe = je.getAsString();
        Log.e(MainActivity.class.getSimpleName(), "strJe=" + strJe);
        Log.e(MainActivity.class.getSimpleName(), "strJe2=" + String.valueOf(je));
        Map<String, Object> map3 = JsonToMapHelper.toMap(jo2);
        Log.e(MainActivity.class.getSimpleName(), "map3=" + map3.toString());
        Object obj3 = map3.get("\nremark");
        Log.e(MainActivity.class.getSimpleName(), "obj3=" + obj3.toString());
        String remark3 = obj3.toString();
        Log.e(MainActivity.class.getSimpleName(), "remark3=" + remark3.toString());


        //test4
        Map<String, Object> map4 = JSONHelper.jsonToMap(json);
        Log.e(MainActivity.class.getSimpleName(), "map4=" + map4.toString());
        Object obj4 = map4.get("\nremark");
        Log.e(MainActivity.class.getSimpleName(), "obj4=" + obj4.toString());
//        String remark = String.valueOf(map.get("remark"));
        String remark4 = String.valueOf(obj4);
        Log.e(MainActivity.class.getSimpleName(), "remark4=" + remark4.toString());
        textView3.setText(remark4);
    }


    private void setListener() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump();
            }
        });
    }

    private void jump() {
        IntentHelperDemo.jump(this);
    }
}
